home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / dialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  3.8 KB  |  104 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        dialogs.c
  4.  
  5. Purpose:    This module handles positioning a dialog on the screen as
  6.             per Human Interface Guidelines.  Also, a quick-and-dirty
  7.             default button outline, and a proc filter for dialogs to
  8.             map RETURN or ENTER to button 1 and ESCAPE or COMMAND-PERIOD
  9.             to button 2.
  10.  
  11. \**********************************************************************/
  12.  
  13. #include "dialogs.h"
  14.  
  15. void PositionDialog(ResType theType, short theID)
  16. /* call this function BEFORE loading an alert or dialog box; this function will
  17.    load the alert/dialog into memory and position it centered horizontally and
  18.    positioned 1:3 vertically.  Then, load the alert/dialog right after the call
  19.    to PositionDialog, and the alert/dialog will be loaded from the copy in
  20.    memory, which has already been positioned correctly.  Cute, eh?  See error.c
  21.    for an example. */
  22. {
  23.     Handle                theTemplate;    /* Handle to resource template    */
  24.     register Rect        *theRect;        /* Bounding box of dialog        */
  25.     register short        left;            /* Left side of centered rect    */
  26.     register short        top;            /* Top side of centered rect    */
  27.     
  28.     /* The first field of the resource template for DLOG's and ALRT's */
  29.     /* is its bounding box.  Get a pointer to this rectangle.  This   */
  30.     /* handle dereferencing is safe since the remaining statements in */
  31.     /* this function do not move memory (assignment and simple math). */
  32.  
  33.     theTemplate = GetResource(theType, theID);
  34.     if (theTemplate == 0)
  35.         return;
  36.     theRect=(Rect*)*theTemplate;    /* bounding rectangle */
  37.     
  38.     left = (screenBits.bounds.right - (theRect->right - theRect->left)) / 2;
  39.     top = (screenBits.bounds.bottom - (theRect->bottom - theRect->top)) / 3;
  40.     if (top < (GetMBarHeight() + 1))    /* don't put it over menu bar */
  41.         top = GetMBarHeight() + 1;
  42.  
  43.     theRect->right += left - theRect->left;
  44.     theRect->left = left;
  45.     theRect->bottom += top - theRect->top;
  46.     theRect->top = top;
  47. }
  48.  
  49. pascal void OutlineDefaultButton(DialogPtr myDlog, short itemNum)
  50. /* Use this as the useritem for a dialog which needs an outlined default button. */
  51. /* Make sure the default button is item 1 in the DITL. */
  52. {
  53.     short            itemType;
  54.     Handle            itemH;
  55.     Rect            box;
  56.     
  57.     GetDItem(myDlog, 1, &itemType, &itemH, &box);
  58.     PenSize(3, 3);
  59.     InsetRect(&box, -4, -4);
  60.     FrameRoundRect(&box, 16, 16);
  61.     PenNormal();
  62. }
  63.  
  64. pascal Boolean ProcOFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  65. /* use this as the proc filter when calling ModalDialog -- it maps RETURN and
  66.    ENTER to button 1, and ESCAPE and COMMAND-PERIOD to button 2.  Of course,
  67.    button 1 should be the OK button and button 2 should be the cancel button. */
  68. {
  69.     unsigned char    theChar;
  70.     short            itemType;
  71.     Handle            itemH;
  72.     Rect            box;
  73.     unsigned long    dummy;
  74.     
  75.     switch (theEvent->what)    /* examine event record */
  76.     {
  77.         case keyDown:    /* keypress */
  78.         case autoKey:
  79.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  80.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  81.             {
  82.                 *theItem=1;        /* as if the user selected item #1 */
  83.                 GetDItem(theDialog, 1, &itemType, &itemH, &box);
  84.                 HiliteControl((ControlHandle)itemH, 1);    /* flash button 1 by highlighting, */
  85.                 Delay(8, &dummy);                        /* waiting 8 ticks, and then */
  86.                 HiliteControl((ControlHandle)itemH, 0);    /* unhighlighting -- believe */
  87.                 return TRUE;                            /* it or not, that's Apple's */
  88.             }                                            /* preferred method */
  89.             if ((theChar==0x1b) ||    /* ESCAPE */
  90.                 ((theEvent->modifiers & cmdKey) && (theChar=='.')))    /* COMMAND-PERIOD */
  91.             {
  92.                 *theItem=2;        /* as if the user selected item #2 */
  93.                 GetDItem(theDialog, 2, &itemType, &itemH, &box);    /* same as above */
  94.                 HiliteControl((ControlHandle)itemH, 1);
  95.                 Delay(8, &dummy);
  96.                 HiliteControl((ControlHandle)itemH, 0);
  97.                 return TRUE;
  98.             }
  99.             break;
  100.     }
  101.     
  102.     return FALSE;    /* no faking, proceed as planned */
  103. }
  104.